package com.biaoyoo.www.personapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.biaoyoo.www.personapp.R;
import com.biaoyoo.www.personapp.constant.ConstantValue;
import com.biaoyoo.www.personapp.core.AppStateTrack;
import com.biaoyoo.www.personapp.utils.LogUtils;

/**
 *
 */
public abstract class BaseActivity extends AppCompatActivity implements Toolbar.OnMenuItemClickListener {

    private Toolbar mBaseToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.d("curclass is "+this.getClass().getSimpleName());
        switch (AppStateTrack.getInstance().mCurentState){
            case ConstantValue.STATE_KILL_BACKGROUD:
                processAppExit();
                break;
            case ConstantValue.STATE_KICK_OUT:
            case ConstantValue.STATE_ONLINE:
            case ConstantValue.STATE_OFFLINE:
                loadViews();
                initDatas();
                break;
        }
    }

    /**
     * 初始化view
     */
    protected abstract void loadViews();

    /**
     * 初始化数据
     */
    protected abstract void initDatas();

    /**
     * 用户被顶下来，多个用户登录，跳转到登录界面
     */
    private void kickOutUser() {
        lanchGoalActivity(LoginActivity.class);
    }

    /**
     * 当系统被后台杀死之后，重启应用
     */
    private void processAppExit() {
        Bundle bundle = new Bundle();
        bundle.putString("isExitApp","1");
        lanchGoalActivity(MainActivity.class,bundle);
    }

    /**
     * 界面之间有参数的跳转
     * @param cls
     * @param bundle
     */
    protected void lanchGoalActivity(Class<?> cls,Bundle bundle){
        Intent intent = new Intent(BaseActivity.this,cls);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     * 界面之间无参数的跳转
     * @param cls
     */
    protected void lanchGoalActivity(Class<?> cls){
        Intent intent = new Intent(BaseActivity.this,cls);
        startActivity(intent);
    }

    public void setContentView(int layoutResId, int titleResId){
        setContentView(layoutResId, titleResId, -1,  ConstantValue.TOOLBAR_MODE_BACK);
    }
    public void setContentView(int layoutResId, int titleResId, int mode ){
        setContentView(layoutResId, titleResId, -1, mode);
    }
    public void setContentView(int layoutResId, int titleResId, int menuId , int mode){
        super.setContentView(layoutResId);
        mBaseToolbar = (Toolbar) findViewById(R.id.toolbar);
        TextView toolbarTitle = (TextView) findViewById(R.id.toolbar_title);
        mBaseToolbar.setTitle("");
        toolbarTitle.setText(titleResId);
        if( ConstantValue.TOOLBAR_MODE_BACK == mode){
            mBaseToolbar.setNavigationIcon(R.mipmap.ic_toolbar_back);
        }
        mBaseToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onNavigationBtnClick();
            }
        });

        setUpMenu(menuId);

    }

    protected void setUpMenu(int menuId) {
        if(null != mBaseToolbar){
            mBaseToolbar.getMenu().clear();
            if(menuId > 0 ){
                mBaseToolbar.inflateMenu(menuId);
                mBaseToolbar.setOnMenuItemClickListener(this);
            }
        }
    }

    protected void onNavigationBtnClick() {
        finish();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if(AppStateTrack.getInstance().checkIfShowGesture()){
            lanchGoalActivity(DeviceLockActivity.class);
        }

        if(AppStateTrack.getInstance().mCurentState == ConstantValue.STATE_KICK_OUT &&
                !AppStateTrack.getInstance().isLoginActivity()){
            kickOutUser();
        }

    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()){
            case R.id.test1:
                LogUtils.d("menu test1.....");
                lanchGoalActivity(SetActivity.class);
                break;
            case R.id.test2:
                LogUtils.d("menu test2.....");
                lanchGoalActivity(SimpleListActivity.class);
                break;
            case R.id.test3:
                LogUtils.d("menu test3.....");
                lanchGoalActivity(SimpleDefineListActivity.class);
                break;
            case R.id.test4:
                LogUtils.d("menu test4.....");
                break;
        }
        return false;
    }
}
